<?php
defined('G_G_LIN') or die("URL is error");
// 51链接首页
class IndexAction extends CommonAction {
	
	/**
		网址列表
	*/
    public function index(){
		$this->_list($map, 20); //网站列表
		$this->assign('title', C('site_title') );

		$this->display();
	}

	/**
		新站加入
	*/
	public function add(){
		if($_POST['dosubmit']==1){
			$Links = M('Links');
			$data = array();
			if(empty($_POST['site_name'])){
				$this->error("网站名称不能为空！");
			}else{
				$data['site_name'] = strval($_POST['site_name']);
			}

			if(empty($_POST['site_link'])){
				$this->error("网站地址不能为空！");
			}else{
				$data['site_link'] = strval($_POST['site_link']);
				$data['site_link'] = trim($data['site_link']);
				if($Links->getBySite_link($data['site_link'])) {
					$this->error('网站地址已经存在！');
				}
			}

			if(empty($_POST['site_pwd'])){
				$this->error("登录密码不能为空！");
			}else{
				$data['site_pwd'] = strval($_POST['site_pwd']);
				$data['site_pwd'] = md5($data['site_pwd']);//登录密码
			}

			if(empty($_POST['site_qq'])){
				$this->error("站长QQ不能为空！");
			}else{
				$data['qq'] = intval($_POST['site_qq']);
			}

			if(empty($_POST['cat_id'])){
				$this->error("网站类型不能为空！");
			}else{
				$data['cat_id'] = intval($_POST['cat_id']);
			}

			$data['ctime']	   = time();
			$data['site_desc'] = empty($_POST['site_desc'])?'' : strval($_POST['site_desc']);
			$data['status']    = 1;

			$id = $Links->add($data);
			if($id>0){
				//新增加入成功后登录
				$data['id'] = $id;
				$this->saveUserSession($data);
				$this->assign("jumpUrl", __URL__.'/info/');
				$this->success("恭喜您，新站加入成功！");
				exit;
			}else{
				$this->error("数据保存失败！");
			}
		}
		
		//网站分类列表
		$Cat = M('Cat');
		$rs = $Cat->where('status=1')->field('id,cat_name')->order('cat_order DESC,id ASC')->select();
		$this->assign('cat_list', $rs );
		$this->assign('title', '新站加入 - 友情链接 - '.C('site_name'));

		$this->display();
	}
	
	/** 
		搜索链接
	*/
	public function search(){

		$this->display();
	}
	
	/** 
		网站信息
	*/
	public function info(){
		//检查是否登录
		$this->_checkLogin();
		$Links = M('Links');
		$uid = $_SESSION[C('USER_AUTH_KEY')];

		//修改操作
		if($_POST['dosubmit'] == 1){
			$data = array();
			$data['id'] = $uid;
			if(empty($_POST['site_name'])){
				$this->error("网站名称不能为空！");
			}else{
				$data['site_name'] = strval($_POST['site_name']);
			}

			if(empty($_POST['site_qq'])){
				$this->error("QQ不能为空！");
			}else{
				$data['qq'] = intval($_POST['site_qq']);
			}

			if(empty($_POST['cat_id'])){
				$this->error("网站类型不能为空！");
			}else{
				$data['cat_id'] = intval($_POST['cat_id']);
			}
			$data['site_desc'] = strval($_POST['site_desc']);
			$Links->save($data);
			$this->success('网站信息修改成功');
			exit;
		}

		$user  = $Links->getById($uid);
		$this->assign('vo' , $user );

		//网站分类列表
		$Cat = M('Cat');
		$rs = $Cat->where('status=1')->field('id,cat_name')->order('cat_order DESC,id ASC')->select();
		$this->assign('cat_list', $rs );
		$this->assign('action_name2', ACTION_NAME );
		$this->assign('title', '网站信息 - 网站管理 - '.C('site_name') );

		$this->display();
	}

	public function password(){
		//检查是否登录
		$this->_checkLogin();
		$Links = M('Links');
		$uid = $_SESSION[C('USER_AUTH_KEY')];
		$user  = $Links->getById($uid);
		$this->assign('vo' , $user );
		
		if($_POST['dosubmit'] == 1){
			//修改密码
			$data = array();
			if(empty($_POST['site_pwd_old'])){
				$this->error("旧密码不能为空！");
			}else{
				if(empty($_POST['site_pwd_new'])){
					$this->error("新密码不能为空！");
				}else{
					if($user['site_pwd'] == md5($_POST['site_pwd_old'])){
						$data['id'] = $uid;
						$data['site_pwd'] =  md5($_POST['site_pwd_new']);
						$Links->save($data);
						$this->success('密码修改成功！');
					}else{
						$this->success('旧密码错误！');
					}
				}

				$data['site_name'] = strval($_POST['site_name']);
			}
		}

		$this->assign('action_name', 'info' );//横幅导航标识
		$this->assign('action_name2', ACTION_NAME );	
		$this->assign('title', '修改密码 - 网站管理 - '.C('site_name'));
		$this->display();
	}

	/**
		交换设置 
	*/
	public function setlinks(){

		//检查是否登录
		$this->_checkLogin();
		$Links  = M('Links');
		$uid	= $_SESSION[C('USER_AUTH_KEY')];

		if($_POST['dosubmit'] == 1){
			$data['site_pr2'] = strval($_POST['site_pr2']);
			$data['site_req'] = strval($_POST['site_req']);
			$data['id'] = $uid;
			$Links->save($data);
			$this->success("修改成功！");
			exit;
		}

		$user   = $Links->getById($uid);
		$this->assign('vo' , $user );

		$this->assign('title', '交换设置 - 网站管理 - '.C('site_name') );
		$this->assign('action_name', 'info' );//横幅导航标识
		$this->assign('action_name2', ACTION_NAME );
		$this->display();
	}

	/** 检查 google PR值*/
	public function check_pagerank(){
		//检查是否登录
		$this->_checkLogin();
		$Links  = M('Links');
		$uid	= $_SESSION[C('USER_AUTH_KEY')];
		$links  = $Links->getById($uid);
		if($links['site_link']){
			$url	= 'http://'.$links['site_link'];
			$html_url = htmlspecialchars($url);
			$rs = checkPagerank($html_url);
			$data = array('id'=>$uid,'site_pr'=>$rs);
			$Links->save($data);
			$this->assign("jumpUrl", __URL__.'/info/');
			$this->success('网站PR值更新成功！');
			exit;
		}
	}
	
	/** alexa 排名更新 */
	public function check_alexa(){
		//检查是否登录
		$this->_checkLogin();
		$Links  = M('Links');
		$uid	= $_SESSION[C('USER_AUTH_KEY')];
		$links  = $Links->getById($uid);
		if($links['site_link']){
			$domain	= $links['site_link'];
			$domain = htmlspecialchars($domain);
			$rank = checkAlexaRank($domain);
			$rs = intval($rank);
			$data = array('id'=>$uid,'site_rank'=>$rs);
			$Links->save($data);
			$this->assign("jumpUrl", __URL__.'/info/');
			$this->success('网站alexa排名更新成功！');
			exit;
		}
	}
	
	/**
		登录页面
		
	*/
	public function login(){

		//检查是否登录
		if(isset($_SESSION[C('USER_AUTH_KEY')])) {
				$this->assign("jumpUrl", __URL__.'/info/');
				$this->success('已经登录！');
				exit;
		}

		if($_POST['dosubmit'] == 1){
			$loginname = empty($_POST['site_link'])?'':strval($_POST['site_link']);
			$loginpwd  = empty($_POST['site_pwd'])?'': strval($_POST['site_pwd']);
			if(empty($loginname)) {
				$this->error('帐号错误！');
			}elseif (empty($loginpwd)){
				$this->error('密码必须！');
			}

			//生成认证条件
			$map				=  array();
			$map['site_link']	= $loginname;
			$map["status"]		= 1;
			$authInfo = $this->_get_user_info($map);//获取用户信息

			if(false === $authInfo || empty($authInfo)) {
				$this->error('帐号不存在或已禁用！');
			}else {
				if($authInfo['site_pwd'] != md5($loginpwd)) {
					$this->error('密码错误！');
				}
				$this->saveUserSession($authInfo);
				$this->assign("jumpUrl", __URL__.'/info/');
				$this->success('登录成功！');
				exit;
			}
		}
		$this->assign('title', '站长登录 - 友情链接 - '.C('site_name') );
		$this->display();
	}

	/**
		用户登出
	*/
    public function logout()
    {
        if(isset($_SESSION[C('USER_AUTH_KEY')])) {
			unset($_SESSION[C('USER_AUTH_KEY')]);
			unset($_SESSION);
			session_destroy();
            $this->assign("jumpUrl", __URL__.'/login/');
            $this->success('登出成功！');
        }else {
            $this->error('已经登出！');
        }
		exit;
    }


}